package com.jonsnowsword.wifiassistant;

import java.util.List;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.jonsnowsword.wifiassistant.GoogleAnalyticsApplication.TrackerName;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AddRecordActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_record);
		Intent intent = this.getIntent(); 
		List<ScanResult> list =intent.getParcelableArrayListExtra("list"); 
        ListView listView = (ListView) findViewById(R.id.listView3);  
        listView.setAdapter(new MyAdapter(this,list));  
        listView.setOnItemClickListener(new OnItemClickListenerImpl());
        
	}
	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		sendGoogleAnalyticsView();
		super.onResume();
	}



	Tracker mTracker;

	public void sendGoogleAnalyticsView()
	{
		// Get tracker.
		mTracker = ((GoogleAnalyticsApplication) getApplication()).getTracker(TrackerName.GLOBAL_TRACKER);

		// Set screen name.
		// Where path is a String representing the screen name.
		mTracker.setScreenName("AddRecordActivity");

		// Send a screen view.
		mTracker.send(new HitBuilders.AppViewBuilder().build());
	}
	
	
    public class MyAdapter extends BaseAdapter {  
  	  
        LayoutInflater inflater;  
        List<ScanResult> list;  
        public MyAdapter(Context context, List<ScanResult> list) {  
            // TODO Auto-generated constructor stub  
            this.inflater = LayoutInflater.from(context);  
            this.list = list;  
        }  
  
        @Override  
        public int getCount() {  
            // TODO Auto-generated method stub  
            return list.size()+1;  
        }  
  
        @Override  
        public Object getItem(int position) {  
            // TODO Auto-generated method stub 
        	if(position!=0)
        	{
        		return list.get(position-1);   
        	}
			return null;
        	
        }  
  
        @Override  
        public long getItemId(int position) {  
            // TODO Auto-generated method stub  
            return position;  
        }  
  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  
        	if(position==0)
        	{
                View view = null;  
                view = inflater.inflate(R.layout.item_wifi_add_list, null);  
                TextView textView = (TextView) view.findViewById(R.id.wifiName);  
                textView.setText("Add new wifi/pass"); 
                return view;  
        	}
        	else
        	{
                View view = null;  
                view = inflater.inflate(R.layout.item_wifi_add_list, null);  
                ScanResult scanResult = list.get(position-1);  
                TextView textView = (TextView) view.findViewById(R.id.wifiName);  
                textView.setText(scanResult.SSID);  
                return view;  
        	}

        }  
  
    }  
    
    
    private class OnItemClickListenerImpl implements OnItemClickListener {



		
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,  long id) 
		{
			if(position!=0)
			{
				final ScanResult scanResult=(ScanResult) arg0.getItemAtPosition(position);
				final EditText  editText=new EditText(AddRecordActivity.this);
				editText.setInputType(0x81);
				new AlertDialog.Builder(AddRecordActivity.this) 
		          .setTitle(scanResult.SSID)
		          .setMessage("Please enter the password")
		          .setView(editText)
		          .setNegativeButton("No", null) 
		          .setPositiveButton("Yes", new OnClickListener() 
		          {
		        	  	@Override
						public void onClick(DialogInterface dialog, int which) 
						{	
		        	  		int a=Integer.parseInt(WifiConnect.pdjm(scanResult.capabilities));
		        	  		String b=editText.getText().toString();
		        	  		if(b.equals(""))
		        	  		{
		        	  			b="empty";
		        	  		}
		        	  		if(a==1)
		        	  			FileUtil.write(scanResult.SSID, b,"");
		        	  		else if(a==2)
		        	  			FileUtil.write(scanResult.SSID, b,"WEP");
		        	  		else
		        	  			FileUtil.write(scanResult.SSID, b,"WPA");
		        	  		setResult(RESULT_OK,null);
		        			finish();
						}				
		          }).show();;

			}
			else{
				showAddDialog();
			}
		}
	}
    
    
    protected void showAddDialog() { 
    	        LayoutInflater factory = LayoutInflater.from(this); 
    	
    	        final View textEntryView = factory.inflate(R.layout.dialog, null); 
    	
    	        final EditText editTextName = (EditText) textEntryView.findViewById(R.id.editTextName); 
    	
    	        final EditText editTextNumEditText = (EditText)textEntryView.findViewById(R.id.editTextNum); 
    	
    	        AlertDialog.Builder ad1 = new AlertDialog.Builder(AddRecordActivity.this); 
    	
    	        ad1.setTitle("new wifi/password:"); 
    	
    	        ad1.setIcon(android.R.drawable.ic_dialog_info); 
    	
    	        ad1.setView(textEntryView); 
    	
    	        ad1.setPositiveButton("yes", new DialogInterface.OnClickListener() { 
    
    	            public void onClick(DialogInterface dialog, int i) { 
    	            	if(editTextName.getText().toString().equals(""))
    	            	{
    	            		new AlertDialog.Builder(AddRecordActivity.this)    
								.setTitle("Error!")
								.setMessage("Wifi name can not be empty")  	
								.setPositiveButton("Yes",null)  
								.show();  
    	            	}
    	            	else if(editTextNumEditText.getText().toString().equals("")){
    	            		FileUtil.write(editTextName.getText().toString(),"empty","");
    	            		setResult(RESULT_OK,null);
    	        			finish();
    	            	}
    	            	else{
        	            	FileUtil.write(editTextName.getText().toString(), editTextNumEditText.getText().toString(),"");
        	            	setResult(RESULT_OK,null);
    	        			finish();
    	            	}

    	            } 
    	
    	        }); 
    	
    	        ad1.setNegativeButton("No", null); 
    	
    	        ad1.show();// ��ʾ�Ի��� 

    	    }

}
